#ifndef __COMM_HPP__
#define __COMM_HPP__

#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include"log.hpp"

using namespace std;

Log log;
const int size = 4096;
const string pathname = "/home/Aries";
const int proj_id = 0x6666;

key_t GetKey()
{
    key_t k = ftok(pathname.c_str(), proj_id);
    if(k < 0)
    {
        log(Fatal, "ftok error: %s", strerror(errno));
        exit(1);
    }
    log(Info, "ftok success, key is 0x%x", k);
    return k;
}

int GetShmHelper(int flag)
{
    int shmid = shmget(GetKey(), size, flag);
    if(shmid < 0)
    {
        log(Fatal, "shared memory created error: %s", strerror(errno));
        exit(2);
    }
    log(Info, "shared memory created success, shm is 0x%x", shmid);
    return shmid;
}

int CreateShm()
{
    return GetShmHelper(IPC_CREAT | IPC_EXCL | 0666);
}

int GetShm()
{
    return GetShmHelper(IPC_CREAT);
}

#endif